package com.tengju.bff.interfaces.app.charge.assembler;

import com.idanchuang.component.base.util.StringUtil;
import com.tengju.bff.interfaces.app.charge.vo.MarketRateVO;
import com.tengju.bff.interfaces.app.charge.vo.MarketTypeVO;
import com.tengju.bff.interfaces.shared.AmountType;
import com.tengju.bff.interfaces.shared.AmountVO;
import com.tengju.bff.interfaces.shared.MobileVO;
import com.tengju.data.domain.model.report.AchievementBonus;
import com.tengju.data.domain.model.report.TeamRatioType;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenhang
 * @Date 2021/1/26
 */
public class MarketAssembler {
    public static MarketTypeVO bonusToTypeVO(AchievementBonus bonus,BigDecimal totalAchievement) {
        if (bonus == null) {
            return null;
        }
        MarketTypeVO marketTypeVO = new MarketTypeVO();
        BigDecimal achievement = bonus.getAchievement().compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : bonus.getAchievement();
        BigDecimal profit = bonus.getProfit().compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : bonus.getProfit();
        marketTypeVO.setMarketIndex(new AmountVO(AmountType.INDEX,achievement));
        marketTypeVO.setAdIndex(new AmountVO(AmountType.INDEX,profit));
        double rate = 0;
        if (totalAchievement.compareTo(BigDecimal.ZERO) != 0) {
            rate = bonus.getAchievement().divide(totalAchievement,4, RoundingMode.HALF_DOWN).doubleValue();
        }
        marketTypeVO.setMarketRate(new AmountVO(AmountType.PERCENT,rate));
        TeamRatioType teamRatioType = TeamRatioType.parseByType(bonus.getTeamRatioType());
        marketTypeVO.setTeamRatioType(bonus.getTeamRatioType());
        if (teamRatioType != null) {
            marketTypeVO.setTypeName(teamRatioType.getName());
        } else {
            marketTypeVO.setTypeName(bonus.getTeamRatioType() + "");
        }
        return marketTypeVO;
    }

    public static MarketRateVO assemblerToRateVO(BigDecimal totalAchievement, List<AchievementBonus> userBonusList, UserInfo starUserInfo, UserInfo remoteUserInfo) {
        if (CollectionUtils.isEmpty(userBonusList)) {
            return null;
        }
        BigDecimal childTotalAchievement = BigDecimal.ZERO;
        BigDecimal childTotalBonus = BigDecimal.ZERO;
        for (AchievementBonus bonus : userBonusList) {
            childTotalAchievement = childTotalAchievement.add(bonus.getAchievement());
            childTotalBonus = childTotalBonus.add(bonus.getProfit());
        }
        childTotalAchievement = childTotalAchievement.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : childTotalAchievement;
        childTotalBonus = childTotalBonus.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : childTotalBonus;

        MarketRateVO marketRateVO = new MarketRateVO();
        marketRateVO.setMarketIndex(new AmountVO(AmountType.INDEX,childTotalAchievement));
        marketRateVO.setAdIndex(new AmountVO(AmountType.INDEX,childTotalBonus));
        String name = null;
        if (starUserInfo != null && StringUtils.isNotBlank(starUserInfo.getName())) {
            name = starUserInfo.getNickName();
        }
        if (StringUtils.isBlank(name) && remoteUserInfo != null && StringUtils.isNotBlank(remoteUserInfo.getName())) {
            name = remoteUserInfo.getNickName();
        }

        String mobile = null;
        if (starUserInfo != null && StringUtils.isNotBlank(starUserInfo.getWholePhoneNumber())) {
            mobile = starUserInfo.getWholePhoneNumber();
        }
        if (StringUtils.isBlank(mobile) && remoteUserInfo != null && StringUtils.isNotBlank(remoteUserInfo.getWholePhoneNumber())) {
            mobile = remoteUserInfo.getWholePhoneNumber();
        }

        String headImg = null;
        if (starUserInfo != null && StringUtils.isNotBlank(starUserInfo.getHeadImage())) {
            headImg = starUserInfo.getHeadImage();
        }
        if (StringUtils.isBlank(headImg) && remoteUserInfo != null && StringUtils.isNotBlank(remoteUserInfo.getHeadImage())) {
            headImg = remoteUserInfo.getHeadImage();
        }

        if (StringUtils.isNotBlank(headImg)) {
            if (headImg.endsWith("?x-image-process=style/webp")) {
                headImg =headImg.replaceAll("\\?x-image-process=style/webp","");
            }
        }
        marketRateVO.setName(name);
        marketRateVO.setMobile(new MobileVO(mobile).toString());
        marketRateVO.setHeadImg(headImg);
        if (starUserInfo != null &&  starUserInfo.getStarLevel() != null) {
            marketRateVO.setStarLevel(starUserInfo.getStarLevel());
            StarLevelEnum starLevelEnum = StarLevelEnum.parseByCode(starUserInfo.getStarLevel());
            marketRateVO.setStarLevelDesc(starLevelEnum == null ? null : starLevelEnum.getDesc());
        } else {
            marketRateVO.setStarLevel(StarLevelEnum.NONE.getCode());
            marketRateVO.setStarLevelDesc(StarLevelEnum.NONE.getDesc());
        }

        double rate = 0;
        if (totalAchievement.compareTo(BigDecimal.ZERO) != 0) {
            rate = childTotalAchievement.divide(totalAchievement,4, RoundingMode.HALF_DOWN).doubleValue();
        }
        marketRateVO.setRate(new AmountVO(AmountType.PERCENT,rate).getValue());
        return marketRateVO;
    }
}
